package dao.jpa;

import dao.AlunoTurmaDAO;
import java.util.List;
import javax.persistence.Query;
import modelo.Aluno;
import modelo.AlunoTurma;
import modelo.Turma;
import util.JPAUtil;

/**
 *
 * @author Jorge
 */
public class AlunoTurmaJpaDAO extends AbstractJpaDao<AlunoTurma, Long> implements AlunoTurmaDAO {

    public AlunoTurmaJpaDAO() {
        super(AlunoTurma.class);
    }

    @Override
    public List<AlunoTurma> getAll() {
        String q = "from AlunoTurma";
        Query query = JPAUtil.getEntityManager().createQuery(q);

        return (List<AlunoTurma>) query.getSingleResult();
    }

    public AlunoTurma getPorId(Long id) {
        String q = "select ad from AlunoTurma ad where ad.id=?";
        Query query = JPAUtil.getEntityManager().createQuery(q);
        query.setParameter(1, id);

        return (AlunoTurma) query.getSingleResult();
    }

    public List<AlunoTurma> getPorAluno(Aluno aluno) {
        String q = "select ad from AlunoTurma ad where ad.aluno=?";
        Query query = JPAUtil.getEntityManager().createQuery(q);
        query.setParameter(1, aluno);

        return query.getResultList();
    }

    public List<AlunoTurma> getPorTurma(Turma turma) {
        String q = "select ad from AlunoTurma ad where ad.turma=?";
        Query query = JPAUtil.getEntityManager().createQuery(q);
        query.setParameter(1, turma);

        return query.getResultList();
    }

    public AlunoTurma getPorAlunoETurma(Aluno aluno, Turma turma) {
        String q = "select ad from AlunoTurma ad where ad.aluno=? and ad.turma=?";
        Query query = JPAUtil.getEntityManager().createQuery(q);
        query.setParameter(1, aluno);
        query.setParameter(2, turma);

        return (AlunoTurma) query.getSingleResult();
    }
}
